package me.common.resource
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.events.Event;
	import flash.utils.Dictionary;
	
	import me.common.resource.ResType;
	import me.common.resource.event.LoadResEvent;
	import me.common.resource.loaders.IResLoader;
	import me.common.resource.loaders.SwfLoader;
	import me.common.resource.loaders.TextLoader;
	
	
	public class Resource
	{
		private static var __instance:Resource;
		private var _configLoader:TextLoader;
		private var _completeNotification:String;

		public function get resList():Dictionary
		{
			return _resList;
		}

		private var _completeFunction:Function;
		private var _resList:Dictionary;
		private var _loadList:Vector.<IResLoader>;
		
		public static function getInstance():Resource
		{
			if(__instance == null){
				__instance = new Resource(new Singleton());
			}
			return __instance;
		}
		
		public function Resource(s:Singleton)
		{
			if(!s){
				throw new Error("请使用getInstance()获取实例");
			} else{
				_resList = new Dictionary();
				_loadList = new Vector.<IResLoader>();
			}
		}
		
		
		/**
		 * 增加一个要载入的资源
		 * 
		 * @param resId		资源id
		 * @param resType	资源类型
		 * @param url 		资源路径
		 */		
		public function appendMultiRes(resId:String, resType:String, url:String):void
		{
			var res:IResLoader;
			
			switch(resType){
				
				case ResType.XML:
					res = new TextLoader();
					break;
				case ResType.SWF:
					res = new SwfLoader();
					break;
				case ResType.IMG:
					res = new SwfLoader();
					break;
			}
			
			if(res != null){
				res.resId = resId;
				res.url = url;
				_loadList.push(res);
			}
		}
		
		
		/**
		 * 开始加载 
		 */		
		public function startLoad(completeFunc:Function):void
		{
			_completeFunction = completeFunc;
			loadNext();
		}
		
		/**
		 * 继续下一个资源加载 
		 */		
		private function loadNext():void
		{
			if(_loadList.length > 0){
				var loader:IResLoader = _loadList.pop();
				loader.load();
				loader.addEventListener(LoadResEvent.LOAD_OK, loadOneOver);
			}
			else
				loadAllOk();
		}
		
		private function loadOneOver(e:LoadResEvent):void
		{
			var res:IResLoader = e.target as IResLoader;
			res.removeEventListener(LoadResEvent.LOAD_OK, loadOneOver);
			_resList[res.resId] = res.getData() as BitmapData;
			loadNext();
		}
		
		/**
		 * 加载全部完成
		 */		
		private function loadAllOk():void
		{
			_completeFunction.apply();
		}
		
		/**
		 * 清除所有 
		 */		
		public function clearAll():void
		{
			_loadList = new Vector.<IResLoader>();
			_resList = new Dictionary();
		}
		
		/**
		 * 获得对应swf资源的loaderInfo
		 * @param id
		 * @return 
		 */		
		public function getSwf(id:String):LoaderInfo
		{
			if(_resList.hasOwnProperty(id))
			{
				var res:IResLoader = _resList[id];
				return res.getData() as LoaderInfo;
			}
			else return null;
		}
		
		public function getXml(id:String):String
		{
			if(_resList.hasOwnProperty(id))
			{
				var res:IResLoader = _resList[id];
				return res.getData();
			}
			else return null;
		}
		//
	}
}

class Singleton
{
	
}